<?php 

namespace app\seller\controller;
use app\common\model\Goods as GoodsModel;
use app\seller\controller\Base;
use app\seller\validate\GoodsValidate;
use app\seller\event\GoodsEvent;
use app\common\utils\JsonService;
use think\facade\View;

class Goods extends Base{

    /**
    * 新增商品
    * @return mixed
    */
    public function create(){
        if($this->request->isajax()){
            $post = $this->request->post();
            $validate = new GoodsValidate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }
            $post['seller_id'] = $this->member_id;
            $post['shop_id'] = $this->shop_id;
            return GoodsEvent::createGoods($post);
        }
        //商品库ID
        $base_goods_id = $this->request->param('base_goods_id');
        if (empty($base_goods_id)) {
            throw new \think\exception\RouteNotFoundException();
        }
        $base_goods = \app\common\model\BaseGoods::getOne(['id'=>$base_goods_id]);

        //获取分类
        $category = \app\common\model\Category::getTree();
        //获取品牌
        $brand = \app\common\model\Brand::getAll();
        //获取供货商
        $suppliers = \app\common\model\Suppliers::getAll(['is_check'=>1]);
        //商品模型
        $goodsType = \app\common\model\GoodsType::getAll();
        return View::fetch('create',[
            'category'=>$category,
            'brand'   =>$brand,
            'suppliers'=>$suppliers,
            'goodsType'=>$goodsType,
            'base_goods'=>$base_goods,
        ]);
    }

    /**
    * 编辑商品
    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new GoodsValidate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return GoodsEvent::updateGoods($post);
        }

        $goods_id = $this->request->get('goods_id');
        $model = $this->findModel($goods_id);
        $model['photo_album'] = !empty($model['photo_album'])?unserialize($model['photo_album']):[];

        //获取分类
        $category = \app\common\model\Category::getTree();
        //获取品牌
        $brand = \app\common\model\Brand::getAll();
        //获取供货商
        $suppliers = \app\common\model\Suppliers::getAll(['is_check'=>1]);
        //商品模型
        $goodsType = \app\common\model\GoodsType::getAll();
        //商品库
        $base_goods = \app\common\model\BaseGoods::getOne(['id'=>$model['base_goods_id']]);
        return View::fetch('update',[
            'model'=>$model,
            'category'=>$category,
            'brand'   =>$brand,
            'suppliers'=>$suppliers,
            'goodsType'=>$goodsType,
            'base_goods'=>$base_goods,
        ]);
    }

    /**
    * 商品列表

    * @return mixed
    */
    public function index(){
        $params = array_filter(request()->param());
        $params['seller_id'] = $this->member_id;
        $dataProvider = GoodsEvent::goodsList($params);
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }


    /*
    * 删除商品
    */
    public function delete(){
        if($this->request->isAjax()){
            $goods_id = $this->request->param('goods_id');
            try{
                $model = $this->findModel($goods_id);
                $model->delete();
            }catch (\Exception $ex){
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success('删除成功');
        }
    }

    /*
     * 获取属性规格
     **/
    public function getGoodsType(){
        if($this->request->isAjax()){
            $goods_id = input('goods_id/d')?input('goods_id/d') : 0;
            $type_id = $this->request->post('type_id');
            $data = GoodsEvent::getAttrSpecInput($type_id,$goods_id);
            return JsonService::success('获取成功',$data);
        }
    }


    /**
     * 动态获取商品规格输入框 根据不同的数据返回不同的输入框
     */
    public function ajaxGetSpecInput(){
        $goods_id = input('goods_id/d') ? input('goods_id/d') : 0;
        //print_r(input('post.spec_arr/a',[[]]));die();
        $str = GoodsEvent::getSpecInput($goods_id ,input('post.spec_arr/a',[[]]));
        exit($str);
    }


    /*
     *获取商品列表
     * */
    public function getGoodsList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where[] = ['is_on_sale','=',1];
        if($goods_id = isset($params['goods_id'])){
            $where[] = ['goods_id','=',$params['goods_id']];
        }
        if($cat_id = isset($params['cat_id'])){
            $where[] = ['cat_id','like',"%{$params['cat_id']}%"];
        }

        if($figure_nu = isset($params['figure_nu'])){
            $where[] = ['figure_nu','like',"%{$params['figure_nu']}%"];
        }

        if($goods_name = isset($params['goods_name'])){
            $where[] = ['goods_name','like',"%{$params['goods_name']}%"];
        }

        $condition = [
            'where' => $where,
            'field' =>'goods_id,shop_id,goods_name,figure_nu,shop_price,original_img,goods_type,sales_sum,store_count,figure_nu,comment_count,is_on_sale',
            'order' => ['goods_id' => 'desc'],
            'paginate' => [
                'page' => $page,
                'query' => $params,
                'list_rows'=>15,
            ],
        ];
        $goods = GoodsModel::getPageAll($condition);

        return $this->fetch('select_goods_list',[
            'dataProvider'=>$goods,
        ]);
    }

    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return GoodsModel::findModel($id, $where);
    }

}
